package com.nowcoder.community.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.CookieStore;

/**
 * @BelongsProject: community
 * @BelongsPackage: com.nowcoder.community.util
 * @Author: Master_XXJS
 * @CreateTime: 2022-09-26  15:27
 * @Description: TODO
 * @Version: 1.0
 */
public class CookieUtil {

    /**
     * 进行cookie的获取，根据请求，以及key来获取对应cookie
     * @param request 当前的请求
     * @param name cookie 的 key
     * @return
     */
    public static String getValue(HttpServletRequest request, String name){
        if (request == null || name ==null){
            throw new IllegalArgumentException("参数为空！");
        }

        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for(Cookie cookie : cookies){
                if (cookie.getName().equals(name)){
                    return cookie.getValue();
                }
            }
        }

        return null;
    }
}

